<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文档上传</title>
    <link href="/static/bootstrap.min.css" rel="stylesheet">
    <style>
        .result-card {
            margin-bottom: 1rem;
            padding: 1rem;
            border: 1px solid #dee2e6;
            border-radius: 0.25rem;
        }
        .loading {
            display: none;
        }
    </style>
</head>
<body>
    <div class="container mt-5">
        <h1 class="mb-4">文档上传</h1>
        <!-- 文件上传部分 -->
        <div class="card mb-4">
            <div class="card-body">
                <h5 class="card-title">文档上传</h5>
                <form id="uploadForm" class="mb-3">
                    <div class="mb-3">
                        <input type="file" class="form-control" id="docxFile" accept=".docx" required>
                    </div>
                    <button type="submit" class="btn btn-primary">
                        <span class="spinner-border spinner-border-sm loading" role="status" aria-hidden="true"></span>
                        上传文档
                    </button>
                </form>
                <div id="uploadStatus" class="alert" style="display: none;"></div>
            </div>
        </div>
    </div>
    <script src="/static/bootstrap.bundle.min.js"></script>
    <script>
        // 文件上传处理
        document.getElementById('uploadForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            const form = e.target;
            const fileInput = form.querySelector('#docxFile');
            const submitBtn = form.querySelector('button[type="submit"]');
            const loading = submitBtn.querySelector('.loading');
            const statusDiv = document.getElementById('uploadStatus');

            if (!fileInput.files[0]) {
                showStatus('请选择文件', 'danger');
                return;
            }

            const formData = new FormData();
            formData.append('file', fileInput.files[0]);

            try {
                loading.style.display = 'inline-block';
                submitBtn.disabled = true;

                const response = await fetch('/upload', {
                    method: 'POST',
                    body: formData
                });

                const data = await response.json();
                if (response.ok) {
                    showStatus(data.message, 'success');
                    form.reset();
                } else {
                    showStatus(data.error, 'danger');
                }
            } catch (error) {
                showStatus('上传过程中发生错误', 'danger');
            } finally {
                loading.style.display = 'none';
                submitBtn.disabled = false;
            }
        });

        function showStatus(message, type) {
            const statusDiv = document.getElementById('uploadStatus');
            statusDiv.className = `alert alert-${type}`;
            statusDiv.textContent = message;
            statusDiv.style.display = 'block';
            setTimeout(() => {
                statusDiv.style.display = 'none';
            }, 5000);
        }
    </script>
</body>
</html>